package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.UserMsgAction;
import com.eportal.domain.Sysopt;
import com.eportal.params.background.TCommunitymsg.TCommunityMsgParam;
import com.eportal.service.SysmenuService;
import com.eportal.service.UserMsgService;
import com.eportal.vo.controller.background.communitymsg.TCommunityMsgVO;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 公告通知管理
 * Created by Saber on 2017/8/22.
 */
@Controller
@RequestMapping(value = "/admin/userMsg")
public class UserMsgConroller {

    private static Logger logger = LoggerFactory.getLogger(UserMsgConroller.class.getName());

    @Resource
    private UserMsgService userMsgService;

    @Resource
    private SysmenuService resourceService;
    private static String LISTVIEWURL="/admin/userMsg_listNoticeView.action";

    /**
     * 公告通知列表
     * @author Saber
     * @Date 2017/8/22 下午1:28
     * @param request
     * @param pageEntity    分页对象
     * @see UserMsgAction#listNoticeView()  //todo 这里之所以链接的是一个view方法 是因为使用的 c:foreach 直接在页面渲染的
     * @return
     */
    @RequestMapping(value = "page.json")
    @ResponseBody
    public JSONObject page(HttpServletRequest request,PageEntity pageEntity){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            PageInfo<TCommunityMsgVO> pageInfo=userMsgService.queryForPage(pageEntity, user, userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);

            //存放按钮
            List<Map<String, Object>> buttonMap = resourceService.getButton(LISTVIEWURL);
            json.put("buttonMap", buttonMap);

            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取公告通知列表异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 公告详情
     * @author Saber
     * @Date 2017/8/22 下午2:55
     * @param request
     * @param sId       sId     t033_community_msg主键
     * @see UserMsgAction#listNoticeView()
     * @return
     */
    @RequestMapping(value = "noticeDetail.json")
    @ResponseBody
    public JSONObject noticeDetail(HttpServletRequest request,Long sId){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            json = userMsgService.noticeDetail(sId, user, userRole);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取公告详情异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 消息保存
     * @author Saber
     * @Date 2017/8/22 下午4:00
     * @param request
     * @param param     参数
     * @see UserMsgAction#add()
     * @return
     */
    @RequestMapping(value = "add.json",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject add(HttpServletRequest request,TCommunityMsgParam param){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            json = userMsgService.add(param, user, userRole);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取公告详情异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 删除消息
     * @author Saber
     * @Date 2017/8/22 下午6:05
     * @param request
     * @param sId       sId     t033_community_msg主键
     * @see UserMsgAction#delete()
     * @return
     */
    @RequestMapping(value = "delete.json")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request,Long sId){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            json = userMsgService.delete(sId, user, userRole);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除消息异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }
}
